package com.baidu.foody.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.baidu.foody.databinding.RecipeItemLayoutBinding
import com.baidu.foody.models.Result
import com.baidu.foody.view.ShimmerAnimationView
import com.bumptech.glide.Glide


class RecipeAdapter: RecyclerView.Adapter<RecipeAdapter.MyViewHolder>() {
    private var foodRecipes:List<Result> = emptyList()

    class MyViewHolder(val binding: RecipeItemLayoutBinding) : RecyclerView.ViewHolder(binding.root) {
        fun bind(model: Result){
            binding.recipeResult  = model
            binding.executePendingBindings()
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        LayoutInflater.from(parent.context).also {
            val binding = RecipeItemLayoutBinding.inflate(it)
            return MyViewHolder(binding)
        }
    }

    override fun getItemCount(): Int {
        return foodRecipes.size
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        holder.bind(foodRecipes[position])
    }

    //DiffUtils
    fun setRecipes(recipes: List<Result>){
        foodRecipes = recipes
        notifyDataSetChanged()
    }
}